/**
 * Creates a new command. Base class for all commands
 * 
 * @constructor
 * @borrows EventEmitter
 */
mindmaps.Command = function() {
	this.id = "BASE_COMMAND";
	this.shortcut = null;
	/**
	 * The handler function.
	 * 
	 * @private
	 * @function
	 */
	this.handler = null;
	this.label = null;
	this.description = null;

	/**
	 * @private
	 */
	this.enabled = false;
};

/**
 * Events that can be emitted by a command object.
 * @namespace
 */
mindmaps.Command.Event = {
	HANDLER_REGISTERED : "HandlerRegisteredCommandEvent",
	HANDLER_REMOVED : "HandlerRemovedCommandEvent",
	ENABLED_CHANGED : "EnabledChangedCommandEvent"
};

mindmaps.Command.prototype = {
	/**
	 * Executes the command. Tries to call the handler function.
	 */
	execute : function() {
		if (this.handler) {
			this.handler();
			if (mindmaps.DEBUG) {
				console.log("handler called for", this.id);
			}
		} else {
			if (mindmaps.DEBUG) {
				console.log("no handler found for", this.id);
			}
		}
	},

	/**
	 * Registers a new handler.
	 * 
	 * @param {Function} handler
	 */
	setHandler : function(handler) {
		this.removeHandler();
		this.handler = handler;
		this.publish(mindmaps.Command.Event.HANDLER_REGISTERED);
	},

	/**
	 * Removes the current handler.
	 */
	removeHandler : function() {
		this.handler = null;
		this.publish(mindmaps.Command.Event.HANDLER_REMOVED);
	},

	/**
	 * Sets the enabled state of the command.
	 * 
	 * @param {Boolean} enabled
	 */
	setEnabled : function(enabled) {
		this.enabled = enabled;
		this.publish(mindmaps.Command.Event.ENABLED_CHANGED, enabled);
	}
};
/**
 * Mixin EventEmitter into command objects.
 */
EventEmitter.mixin(mindmaps.Command);

/**
 * Node commands
 */

/**
 * Creates a new CreateNodeCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.CreateNodeCommand = function() {
	this.id = "CREATE_NODE_COMMAND";
	this.shortcut = "tab";
	this.label = "Agregar";
	this.icon = "ui-icon-plusthick";
	this.description = "Crea un nuevo nodo";
};
mindmaps.CreateNodeCommand.prototype = new mindmaps.Command();

/**
 * Creates a new CreateSiblingNodeCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.CreateSiblingNodeCommand = function() {
	this.id = "CREATE_SIBLING_NODE_COMMAND";
	this.shortcut = "shift+tab";
	this.label = "Agregar";
	this.icon = "ui-icon-plusthick";
	this.description = "Creates a new sibling node";
};
mindmaps.CreateSiblingNodeCommand.prototype = new mindmaps.Command();

/**
 * Creates a new DeleteNodeCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.DeleteNodeCommand = function() {
	this.id = "DELETE_NODE_COMMAND";
	this.shortcut = "del";
	this.label = "Eliminar";
	this.icon = "ui-icon-minusthick";
	this.description = "Elimina el nuevo nodo";
};
mindmaps.DeleteNodeCommand.prototype = new mindmaps.Command();

/**
 * Creates a new EditNodeCaptionCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.EditNodeCaptionCommand = function() {
	this.id = "EDIT_NODE_CAPTION_COMMAND";
	this.shortcut = "F2";
	this.label = "Editar Nodo";
	this.description = "Edita el texto del nodo";
};
mindmaps.EditNodeCaptionCommand.prototype = new mindmaps.Command();

/**
 * Creates a new ToggleNodeFoldedCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.ToggleNodeFoldedCommand = function() {
	this.id = "TOGGLE_NODE_FOLDED_COMMAND";
	this.shortcut = "space";
	this.description = "Show or hide the node's children";
};
mindmaps.ToggleNodeFoldedCommand.prototype = new mindmaps.Command();

/**
 * Undo commands
 */

/**
 * Creates a new UndoCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.UndoCommand = function() {
	this.id = "UNDO_COMMAND";
	this.shortcut = "ctrl+z";
	this.label = "Deshacer";
	this.icon = "ui-icon-arrowreturnthick-1-w";
	this.description = "Deshacer acción";
};
mindmaps.UndoCommand.prototype = new mindmaps.Command();

/**
 * Creates a new RedoCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.RedoCommand = function() {
	this.id = "REDO_COMMAND";
	this.shortcut = "ctrl+y";
	this.label = "Rehacer";
	this.icon = "ui-icon-arrowreturnthick-1-e";
	this.description = "Rehacer acción";
};
mindmaps.RedoCommand.prototype = new mindmaps.Command();

/**
 * Clipboard commands
 */

/**
 * Creates a new CopyNodeCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.CopyNodeCommand = function() {
	this.id = "COPY_COMMAND";
	this.shortcut = "ctrl+c";
	this.label = "Copiar";
	this.icon = "ui-icon-copy";
	this.description = "Copiar un arco";
};
mindmaps.CopyNodeCommand.prototype = new mindmaps.Command();

/**
 * Creates a new CutNodeCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.CutNodeCommand = function() {
	this.id = "CUT_COMMAND";
	this.shortcut = "ctrl+x";
	this.label = "Cortar";
	this.icon = "ui-icon-scissors";
	this.description = "Cortar un arco";
};
mindmaps.CutNodeCommand.prototype = new mindmaps.Command();

/**
 * Creates a new PasteNodeCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.PasteNodeCommand = function() {
	this.id = "PASTE_COMMAND";
	this.shortcut = "ctrl+v";
	this.label = "Pegar";
	this.icon = "ui-icon-clipboard";
	this.description = "Pegar un arco";
};
mindmaps.PasteNodeCommand.prototype = new mindmaps.Command();

/**
 * Document commands
 */

/**
 * Creates a new NewDocumentCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.NewDocumentCommand = function() {
	this.id = "NEW_DOCUMENT_COMMAND";
	this.label = "Nuevo";
	this.icon = "ui-icon-document-b";
	this.description = "Empezar a trabajar desde cero";
};
mindmaps.NewDocumentCommand.prototype = new mindmaps.Command();

/**
 * Creates a new OpenDocumentCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.OpenDocumentCommand = function() {
	this.id = "OPEN_DOCUMENT_COMMAND";
	this.label = "Abrir...";
	this.shortcut = "ctrl+o";
	this.icon = "ui-icon-folder-open";
	this.description = "Abrir el hipermedio";
};
mindmaps.OpenDocumentCommand.prototype = new mindmaps.Command();

/**
 * Creates a new SaveDocumentCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.SaveDocumentCommand = function() {
	this.id = "SAVE_DOCUMENT_COMMAND";
	this.label = "Guardar...";
	this.shortcut = "ctrl+s";
	this.icon = "ui-icon-disk";
	this.description = "Guardar el hipermedio";
};
mindmaps.SaveDocumentCommand.prototype = new mindmaps.Command();

/**
 * Creates a new CloseDocumentCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.CloseDocumentCommand = function() {
	this.id = "CLOSE_DOCUMENT_COMMAND";
	this.label = "Cerrar";
	this.icon = "ui-icon-close";
	this.description = "Cerrar el hipermedio";
};
mindmaps.CloseDocumentCommand.prototype = new mindmaps.Command();

/**
 * Creates a new HelpCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.HelpCommand = function() {
	this.id = "HELP_COMMAND";
	this.enabled = true;
	this.icon = "ui-icon-help";
	this.label = "Ayuda";
	this.shortcut = "F1";
	this.description = "Obtener ayuda";
};
mindmaps.HelpCommand.prototype = new mindmaps.Command();

/**
 * Creates a new PrintCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.PrintCommand = function() {
	this.id = "PRINT_COMMAND";
	this.icon = "ui-icon-print";
	this.label = "Imprimir";
	this.shortcut = "ctrl+p";
	this.description = "Imprimir el hipermedio";
};
mindmaps.PrintCommand.prototype = new mindmaps.Command();

/**
 * Creates a new ExportCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.ExportCommand = function() {
	this.id = "EXPORT_COMMAND";
	this.icon = "ui-icon-image";
	this.label = "Guardar como imagen...";
	this.description = "Exportar el hipermedio";
};
mindmaps.ExportCommand.prototype = new mindmaps.Command();

/**
 * Creates a new AbrirNodoCommand.
 * 
 * @constructor
 * @augments mindmaps.Command
 */
mindmaps.OpenNodeCommand = function() {
	this.id = "OPEN_NODE_COMMAND";
	this.icon = "ui-icon-image";
	this.label = "Abrir nodo...";
	this.description = "Exportar el hipermedio";
};
mindmaps.OpenNodeCommand.prototype = new mindmaps.Command();